<template>
  <div>
    <div>&nbsp;</div>
    <div>&nbsp;</div>
    <h1>{{ 'Test Model' }}</h1>

    <div>&nbsp;</div>
    <button @click="onclick"><H1>Tree View</H1></button>

    <div>&nbsp;</div>
    <div>&nbsp;</div>

    <h6>Tree View 列表页面</h6>

    <a-table :columns="columns" :data-source="dataList" rowKey="id"> </a-table>

    <div>&nbsp;</div>
    <div>&nbsp;</div>

    <div>&nbsp;</div>
    <button @click="onclickLogin"><H1>Login</H1></button>

    <div>
      <h2>-- session_info.server_version---</h2>
      {{ session_info.server_version }}

      <h2>-- session_info.uid---</h2>
      {{ session_info.uid }}

      <h2>-- session_info.username---</h2>
      {{ session_info.username }}

      <h2>-- session_info.name---</h2>
      {{ session_info.name }}

      <h2>-- context---</h2>
      {{ context }}
    </div>
  </div>
</template>

<script>
import api from '@/odoorpc'

// const baseURL = 'http://192.168.56.108:8069'
const baseURL = process.env.VUE_APP_BASE_API
const timeout = 50000
api.init({ baseURL, timeout })

export default {
  name: 'TestDataset',
  props: {},
  data() {
    return {
      session_info: {},
      context: {},

      fields_info: {},

      dataList: []
    }
  },
  computed: {
    columns() {
      const cols = [
        { dataIndex: 'name', key: 'name', title: 'Name' },
        { dataIndex: 'display_name', key: 'display_name', title: '全名' },
        { dataIndex: 'type', key: 'type', title: '内部类型' },
        { dataIndex: 'barcode', key: 'barcode', title: '条码' },
        { dataIndex: 'default_code', key: 'default_code', title: '编码' },
        { dataIndex: 'categ_id', key: 'categ_id', title: '类别' }
      ]

      const get_render = col => {
        const meta = this.fields_info[col] || {}
        if (meta.type === 'many2one') {
          // eslint-disable-next-line no-unused-vars
          return (value, row, index) => (value ? value[1] : '')
        }
        if (meta.type === 'selection') {
          const get_label = value => {
            const elm = meta.selection.find(item => item[0] === value)
            return elm ? elm[1] : ''
          }
          // eslint-disable-next-line no-unused-vars
          return (value, row, index) => (value ? get_label(value) : '')
        }
        return undefined
      }

      return cols.map(col => {
        const col2 = {}
        const render = get_render(col.dataIndex)
        if (render) col2.customRender = render
        return { ...col, ...col2 }
      })
    }
  },

  async created() {},
  methods: {
    async onclick() {
      const fields = [
        'name',
        'display_name',
        'type',
        'barcode',
        'default_code',
        'categ_id'
      ]

      // env.model 这个函数升级.
      // 如果 传第二个参数 {fields}, 则函数变为异步函数, 且函数内 会 调用 fields_get 获取字段信息
      // Model._fields 属性中, 暂存 字段信息
      const Model = await api.env.model('product.template', { fields })

      this.fields_info = Model._fields

      console.log([Model])

      // Model._fields 属性中, 若有该字段的信息, 则 查询返回值, 将额外做非空处理
      const result = await Model.search_read({
        domain: [],
        fields
        // limit: 8,
        // offset: this.offset
      })

      console.log('result:', result)

      this.dataList = result
    },

    async onclickLogin() {
      console.log('click btn')
      const kwargs = { db: 'T1', login: 'admin', password: '123456' }
      const result = await api.web.session.authenticate(kwargs)
      console.log('result:', result)
      this.session_info = result
      this.context = api.web.session.context
    }
  }
}
</script>

<style scoped></style>
